package com.example.areaclean;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.areaclean.database.Student;

import org.json.JSONObject;

import java.util.HashMap;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class student_activity extends AppCompatActivity {
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    RequestQueue queue;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student);

        TextView textView3=findViewById(R.id.text_stu);
        Button check =findViewById(R.id.btn_stu_qd);
        Button classroom =findViewById(R.id.btn_stu_area);
        Button score =findViewById(R.id.btn_stu_score);

        queue = Volley.newRequestQueue(getApplicationContext());
        pref = getSharedPreferences("data",MODE_PRIVATE);
        jsonRequest("find_stuo",pref.getString("userNo",""));

        //启动学生打卡签到页面
        check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(student_activity.this,stu_check_activity.class);
                startActivity(intent);
            }
        });
        //启动学生查询教室页面
       classroom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(student_activity.this,stu_area_activity.class);
                startActivity(intent);
            }
        });
        //启动学生查询分数页面
        score.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(student_activity.this,stu_score_activity.class);
                startActivity(intent);
            }
        });
    }

    public void jsonRequest(String aim, final String userNo){//通过userNo 查询student信息
        String url= this.getString(R.string.recUrl);

        HashMap<String, String> params = new HashMap<>();
        params.put("forwhat",aim);
        params.put("userNo",userNo);
        org.json.JSONObject jsonObject =new JSONObject(params);
//        JSONObject jsonObject = new JSONObject(params);

        Log.d("Tag",jsonObject.toString());
        JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<org.json.JSONObject>() {
                    @Override
                    public void onResponse(org.json.JSONObject jsonObject) {
                        Student stuo = JSON.parseObject(jsonObject.toString(),Student.class);//解析json
                        Log.d("TAG1",jsonObject.toString());
                        if (userNo.equals(stuo.getS_no())){//java判断字符串相等不能用“==”
                            //存储到pref
                            SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
                            editor.putString("s_no",stuo.getS_no());
                            editor.putString("s_name1",stuo.getS_name());
                            editor.putString("s_class1",stuo.getS_class());
                            editor.putString("s_institute",stuo.getS_institute());
                            editor.apply();

                            TextView textView =(TextView)findViewById(R.id.text_stu);
                            textView.setText(stuo.getS_name());
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.d("TAG","fuckError");
                    }
                });
        queue.add(jsObjRequest);
    }
}
